package com.rrd.spring.event.router;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;

import java.lang.reflect.Method;

/**
 * A java reflection API implementation of {@link Invoker}
 *
 * @author eugene_rachitskiy
 */
public class ReflectionInvoker implements Invoker {
    private static final Logger log = LoggerFactory.getLogger(ReflectionInvoker.class);
    private final Object instance;
    private final Method method;

    public ReflectionInvoker(Object instance, Method method) {
        this.instance = instance;
        this.method = method;
        method.setAccessible(true);
    }

    public void invoke(ApplicationEvent event) {
        try {
            method.invoke(instance,event);
        } catch (Exception e) {
            log.error("Could not invoke event handling method",e);
        }
    }
}
